<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- <img class="logoPic" src="../assets/logo.png" alt="" /> -->
        <span>门票销售管理系统</span>
      </div>
      <!-- 头像区域 -->
      <div class="header_avatar">
        <div class="demo-basic--circle">
          <div class="block">
            <el-avatar v-show="circleUrl" :size="50" :src="circleUrl"></el-avatar>
            <el-avatar v-show="!circleUrl" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
          </div>
        </div>
        <span class="avatar_title"
          ><h6>您好,{{ uname }}</h6></span
        >
        <el-button type="danger" @click="logout">退出</el-button>
      </div>
      <!-- <el-button type="danger" @click="logout">退出</el-button> -->
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#ffffff" text-color="rgb(3, 101, 100)" >
          <!-- 一级菜单 -->
          <el-submenu :index="id+''" v-for="(item,id) in menus" :key="id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + id" v-for="(subItem,id) in item.children" :key="id" @click="$router.push('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-link"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'Home',
  data() {
    return {
      // 默认头像
      touxiang: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
      circleUrl: '',
      uname: '',
      menus: [],
       iconsObj: {
        '1': 'el-icon-s-goods',
        '2': 'el-icon-open',
        '3': 'el-icon-s-marketing',
        '4': 'el-icon-s-custom',
      },
    }
  },
  created() {
    this.getUserInfo()
    // 初始化menus
    this.menus = this.rightList
    this.uname = this.username
    this.circleUrl = this.user_pic
  },
  computed: {
    ...mapState(['rightList', 'username','user_pic'])
  },
  methods: {
    logout() {
      // 退出登录清空token 跳转到登录页面
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getUserInfo() {
      const { data: res } = await this.$http.get('my/userinfo')
      if (res.status !== 0) return this.$message.error(res.message)
      this.circleUrl = res.data.user_pic
      this.$store.commit('setUserPic', res.data.user_pic)
    }
  },
}
</script>

<style lang="less" scoped>
.avatar_title {
  margin-right: 20px;
}
.header_avatar {
  float: right;
}
.userBtn {
  float: left;
}
.logoPic {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  overflow: hidden;
}

.home-container {
  height: 100%;
}

.el-header {
  background-color: rgb(3, 101, 100);
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: rgb(238, 241, 246);
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
